//
//  GTNewFeatureController.swift
//  swiftWEIbo
//
//  Created by mac－gt on 15/11/25.
//  Copyright © 2015年 mac－gt. All rights reserved.
//

import UIKit


class GTNewFeatureController: UICollectionViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
           self.collectionView?.registerClass(GTNewFeatureCell.self, forCellWithReuseIdentifier: "GTNewFeatureCell")
        layout.itemSize=view.bounds.size
        layout.minimumInteritemSpacing=0
        layout.minimumLineSpacing=0
        layout.scrollDirection=UICollectionViewScrollDirection.Horizontal
        collectionView?.pagingEnabled=true
        collectionView?.bounces=false
        collectionView?.showsHorizontalScrollIndicator=true
    
          }
    let layout=UICollectionViewFlowLayout()
    var itemCount=4
    init(){
        super.init(collectionViewLayout: layout)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return itemCount
    }
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell=collectionView.dequeueReusableCellWithReuseIdentifier("GTNewFeatureCell", forIndexPath: indexPath) as! GTNewFeatureCell
        cell.imageIndex=indexPath.item
        return cell
    }
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        let indexPath=collectionView.indexPathsForVisibleItems().last
        if indexPath?.item==itemCount-1
        {
            let cell = collectionView.cellForItemAtIndexPath(indexPath!) as! GTNewFeatureCell

            cell.showButtonAni()
        }
    }
    }

class GTNewFeatureCell: UICollectionViewCell {
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUp()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    var imageIndex: Int? = 0 {
        didSet{
            startButton.hidden=true
            bgView.image=UIImage(named:"new_feature_\(imageIndex! + 1)")
        }
    }
    private lazy var bgView=UIImageView()
    private lazy var startButton:UIButton={
        let button=UIButton()
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        button.setTitle("开始体验", forState: UIControlState.Normal)
        button.addTarget(self, action: "startButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        return button
    }()
    // 动画显示开始体验按钮
    func showButtonAni(){
        startButton.hidden=false
        startButton.transform=CGAffineTransformMakeScale(0, 0)
        UIView.animateWithDuration(1.2, delay: 0.1, usingSpringWithDamping: 0.6, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            self.startButton.transform=CGAffineTransformIdentity
            }) { (_) -> Void in
                
        }
    }
    //开始体验按纽点击方法
    func startButtonClick(){
        (UIApplication.sharedApplication().delegate as! AppDelegate).switchRootController(true)
    }
    //添加子控件和约束
    func setUp(){
        contentView.addSubview(bgView)
        contentView.addSubview(startButton)
        bgView.translatesAutoresizingMaskIntoConstraints=false
        startButton.translatesAutoresizingMaskIntoConstraints=false
        contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[bg]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["bg" : bgView]))
        contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[bg]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["bg" : bgView]))
        contentView.addConstraint(NSLayoutConstraint(item: startButton, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        contentView.addConstraint(NSLayoutConstraint(item: startButton, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -160))
    }
}